package com.wie.staffhubpro.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wie.staffhubpro.domain.dto.EmpDTO;
import com.wie.staffhubpro.domain.entity.Emp;
import com.wie.staffhubpro.service.EmpService;
import com.wie.staffhubpro.mapper.EmpMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author WiedersehenM
 * @description 针对表【emp】的数据库操作Service实现
 * @createDate 2025-06-26 23:28:35
 */
@Service
public class EmpServiceImpl extends ServiceImpl<EmpMapper, Emp>
        implements EmpService {

    @Autowired
    private EmpMapper empMapper;

    @Override
    public List<Emp> listEmp() {
        QueryWrapper<Emp> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", "在职");
        List<Emp> empList = empMapper.selectList(queryWrapper);
        return empList;
    }

    /**
     * 根据部门id查询员工
     */
    public List<Emp> getByDeptId(Integer deptId) {
        QueryWrapper<Emp> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dept_id", deptId);
        List<Emp> empList = empMapper.selectList(queryWrapper);
        return empList;
    }

    /**
     * 根据角色id查找员工
     */
    public List<Emp> getByRoleId(String roleId) {
        QueryWrapper<Emp> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id", roleId);
        List<Emp> empList = empMapper.selectList(queryWrapper);
        return empList;
    }

    /**
     * 根据状态查询员工
     */
    public List<Emp> getByStatus(String status) {
        QueryWrapper<Emp> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", status);
         List<Emp> empList = empMapper.selectList(queryWrapper);
        return empList;
    }

    /**
     * 根据员工姓名模糊查询员工
     */
    public List<Emp> getByName(String empName) {
        QueryWrapper<Emp> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("emp_name", empName);
        List<Emp> empList = empMapper.selectList(queryWrapper);
        return empList;
    }

    /**
     * 通过deptId查找部长
     */
    public List<Emp> getBossByDeptId(Integer deptId) {
        QueryWrapper<Emp> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dept_id", deptId)
                .eq("role_id", 2);
        List<Emp> empList = empMapper.selectList(queryWrapper);
        return empList;
    }

    /**
     * 新增员工
     */
    public void addEmp(EmpDTO empDTO) {
        // 按照格式设置员工id，首位为部门id
        QueryWrapper<Emp> queryWrapper = new QueryWrapper<>();
        // 根据id倒序排列员工
        queryWrapper.eq("dept_id", empDTO.getDeptId())
                .orderByDesc("id");
        List<Emp> empList = empMapper.selectList(queryWrapper);
        // 找到该部门的最大id，再加一即为新员工的id
        Emp dbEmp = empList.get(0);
        Integer dbEmpId = dbEmp.getId();
        Emp emp = new Emp();
        BeanUtils.copyProperties(empDTO, emp);
        emp.setId(dbEmpId + 1);
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.insert(emp);
    }

    /**
     * 根据id修改员工信息
     * @param emp
     */
    public void alterEmp(Emp emp) {
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.updateById(emp);
    }
}




